iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 14
1
AI & Data

AI&Machine Learning系列 第 14

(不專業AI的介紹) 機器學習-Machine-Learning -> 相關功能介紹 -> 決策樹 Day14

  • 分享至 

  • xImage
  •  

上次有跟各位介紹到 Regression 以及 Classification 這次來跟大家介紹關於決策樹這個方法,其實決策樹大家可以想得簡單的一點,就像是我們的電腦資料夾一樣,一個大的資料夾底下帶著許多小資料夾,那形成一個巨大的樹木的感覺,這就是決策樹簡單的概念,那我們廢話不多說,直接給他開始。

這次的參考先謝謝 https://www.itread01.com/p/527135.html python 教程小小的介紹,https://www.itread01.com/cieee.html 對於決策樹將內容說明的詳細,特徵,資料,做法皆在裏頭,以及https://medium.com/jameslearningnote/%E8%B3%87%E6%96%99%E5%88%86%E6%9E%90-%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92-%E7%AC%AC3-5%E8%AC%9B-%E6%B1%BA%E7%AD%96%E6%A8%B9-decision-tree-%E4%BB%A5%E5%8F%8A%E9%9A%A8%E6%A9%9F%E6%A3%AE%E6%9E%97-random-forest-%E4%BB%8B%E7%B4%B9-7079b0ddfbda 本網站對於決策樹 以及 隨機森林的詳細公式介紹,因參考資料上的程式有詳細的說明,所以本人在此對於決策樹,做一個敘述跟探討。

簡單來說,決策樹是一個特徵性的東西,我們今天要對一個東西做學習,不是死命地學她做甚麼事情,而且這個東西有一些技巧性的問題,而這個技巧性,就是他所說的特徵,因為如果今天學習一項東西,如果不針對特徵做學習,那這個東西的分類意義就沒有太大的幫助,如果今天有兩大堆的東西在您面前需要您分辨說,哪一個是好的,哪一個是壞的,但是好跟壞一定有一個標準,而這個標準也可以稱為特徵,大大小小的事情都可以被拿來做分類,只要有特徵性做出來就是一個正確的方向。

from matplotlib.colors import ListedColormap

def plot_decision_regions(X, y, classifier, test_idx=None, resolution=0.02):

    # setup marker generator and color map
    markers = ('s', 'x', 'o', '^', 'v')
    colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
    cmap = ListedColormap(colors[:len(np.unique(y))])

    # plot the decision surface
    x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
                           np.arange(x2_min, x2_max, resolution))
    Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
    Z = Z.reshape(xx1.shape)
    plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap)
    plt.xlim(xx1.min(), xx1.max())
    plt.ylim(xx2.min(), xx2.max())

    for idx, cl in enumerate(np.unique(y)):
        plt.scatter(x=X[y == cl, 0], 
                    y=X[y == cl, 1],
                    alpha=0.6, 
                    c=cmap(idx),
                    edgecolor='black',
                    marker=markers[idx], 
                    label=cl)

    # highlight test samples
    if test_idx:
        # plot all samples
        if not versiontuple(np.__version__) >= versiontuple('1.9.0'):
            X_test, y_test = X[list(test_idx), :], y[list(test_idx)]
            warnings.warn('Please update to NumPy 1.9.0 or newer')
        else:
            X_test, y_test = X[test_idx, :], y[test_idx]

        plt.scatter(X_test[:, 0],
                    X_test[:, 1],
                    c='',
                    alpha=1.0,
                    edgecolor='black',
                    linewidths=1,
                    marker='o',
                    s=55, label='test set')

https://medium.com/jameslearningnote/%E8%B3%87%E6%96%99%E5%88%86%E6%9E%90-%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92-%E7%AC%AC3-5%E8%AC%9B-%E6%B1%BA%E7%AD%96%E6%A8%B9-decision-tree-%E4%BB%A5%E5%8F%8A%E9%9A%A8%E6%A9%9F%E6%A3%AE%E6%9E%97-random-forest-%E4%BB%8B%E7%B4%B9-7079b0ddfbda 這篇大大上面程式所寫,將資料一併抓進來之後,在一步一步地做分析,最後用圖形來表示,有興趣者可以參考以上三篇大大的程式,獲益良多,因本人對於決策樹事實上不大了解,所以也是觀察各位大大的文章得知。

下一篇會以本人有做的打磚塊的一個小遊戲來做說明,以上也謝謝各位捧場,以上為不專業的AI介紹,那我們下篇見~~~

參考資料:https://www.itread01.com/p/527135.html
https://www.itread01.com/cieee.html
https://medium.com/jameslearningnote/%E8%B3%87%E6%96%99%E5%88%86%E6%9E%90-%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92-%E7%AC%AC3-5%E8%AC%9B-%E6%B1%BA%E7%AD%96%E6%A8%B9-decision-tree-%E4%BB%A5%E5%8F%8A%E9%9A%A8%E6%A9%9F%E6%A3%AE%E6%9E%97-random-forest-%E4%BB%8B%E7%B4%B9-7079b0ddfbda


上一篇
(不專業的AI介紹) 機器學習-Machine-Learning -> 相關功能介紹(Regression) Day 13
下一篇
(不專業的AI介紹) 機器學習-Machine-Learning -> 打磚塊(non-Machine-Learning) Day 15
系列文
AI&Machine Learning30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言